/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.reportCenter.movementReport;

import com.openbrew.openbrewrf.Employee;
import com.openbrew.openbrewrf.Equipment;
import com.openbrew.openbrewrf.Fv;
import com.openbrew.openbrewrf.Movement;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import service.enumerations.MovementState;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.movementreport")
public class MovementReportREST {

    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public MovementReportREST() {
    }

    @GET
    @Path("{batchID}")
    @Produces(MediaType.APPLICATION_XML)
    public List<MovementReport> find(@PathParam("batchID") Integer batchID) {
        return runReport(batchID);
    }

    private List<MovementReport> runReport(final int batchID) {
        List<Movement> movements = getAllBatchMovements(em, batchID);
        List<MovementReport> reports = new ArrayList<MovementReport>();
        for(final Movement mvmt : movements){
            String fromVessel = "";
            String toVessel = "";
            String employee = "";
            if(mvmt.getMovementEmpID()!=0){
                final Employee emp = getEmployeeFromMovement(em, mvmt);
                employee = emp.getEmpLastName() + ", " + emp.getEmpFirstName();
            }
            //Vessels
            if(mvmt.getMovementOVID()!=0){
                final Fv vessel = getVessel(em, mvmt.getMovementOVID());
                fromVessel = vessel.getFVDesg() + ": " + vessel.getFVName();
            }
            if(mvmt.getMovementNVID()!=0){
                final Fv vessel = getVessel(em, mvmt.getMovementNVID());
                toVessel = vessel.getFVDesg() + ": " + vessel.getFVName();
            }
            
            String oldState = MovementState.getState(mvmt.getMovementOldState()).toString();
            String newState = MovementState.getState(mvmt.getMovementStateID()).toString();

            
            String dispStr = "Batch: " + Integer.toString(batchID) + " - Movement History\n\n";
            dispStr += "Movement ID: " + Integer.toString(mvmt.getMovementID()) + "\n";
            //Movement States
            dispStr += "State: " + oldState + " to " + newState + "\n";
            dispStr += "Moved From: " + fromVessel;
            if(!toVessel.equals("")){
                dispStr += " To: " + toVessel;
            }
            dispStr += "\n";
            dispStr += "Date: " + mvmt.getMovementDate().toString() + "\n";
            dispStr += "Volume: " + Float.toString(mvmt.getMovementVolume()) + "\n";
            dispStr += "By: " + employee + "\n";
            dispStr += "Notes: " + mvmt.getMovementNotes() + "\n";
            if(mvmt.getEquipmentCollection()!=null){
                dispStr += "Equipment used:\n";
                for(final Equipment equip : mvmt.getEquipmentCollection()){
                    dispStr += "     " + equip.getEquipName() + " (" + equip.getEquipSerialNum() + ")\n";
                }
            }
            dispStr += "\n\n\n";
                
            reports.add(new MovementReport(mvmt, dispStr, employee, fromVessel, toVessel));
        }
        return reports;
    }

    /*************************************************************************
     * Database access files.
     *************************************************************************/
    private List<Movement> getAllBatchMovements(final EntityManager en, final int batchID) {
        TypedQuery<Movement> query = en.createQuery(
                "SELECT m FROM Movement m WHERE m.movementBrewID = ?1", Movement.class);
        query.setParameter(1, batchID);
        return query.getResultList();
    }
    
    private Employee getEmployeeFromMovement(final EntityManager en, final Movement mvmt) {
        TypedQuery<Employee> query = en.createQuery(
                "SELECT e FROM Employee e WHERE e.empID = ?1", Employee.class);
        query.setParameter(1, mvmt.getMovementEmpID());
        return query.getSingleResult();
    }
    
    private Fv getVessel(final EntityManager en, final int vesselID) {
        TypedQuery<Fv> query = en.createQuery(
                "SELECT f FROM Fv f WHERE f.fvid = ?1", Fv.class);
        query.setParameter(1, vesselID);
        return query.getSingleResult();
    }

    protected EntityManager getEntityManager() {
        return em;
    }
    
}
